#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>

static void sig_handler(int sig){
    puts("Alarm timeout");
    exit(0);
}

int main(int argc, char **argv)
{
    struct sigaction sig = {0};
    int second;

    if(2 > argc){
        exit(-1);
    }

    sig.sa_handler = sig_handler;
    sig.sa_flags = 0;

    if (-1 == sigaction(SIGALRM, &sig, NULL)) {
        perror("sigaction error");
        exit(-1);
    }

    second = atoi(argv[1]);
    printf("定时时长: %d 秒\n", second);
    alarm(second);

    for(;;)
        sleep(1);

    exit(0);
}